package com.ruoyi.project.wms.essentials.purchase.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.wms.essentials.purchase.domain.WmsPurchase;
import com.ruoyi.project.wms.essentials.purchase.service.IWmsPurchaseService;
import com.ruoyi.project.wms.product.domain.WmsProduct;
import com.ruoyi.project.wms.product.service.IWmsProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 进货订单Controller
 * 
 * @author ruoyi
 * @date 2020-10-29
 */
@Controller
@RequestMapping("/essentials/purchase")
public class WmsPurchaseController extends BaseController
{
    private String prefix = "wms/essentials/purchase";

    @Autowired
    private IWmsPurchaseService wmsPurchaseService;

    @Autowired
    private IWmsProductService wmsProductService;

    /**
     * 跳转订货页面
     * @return
     */
    @RequiresPermissions("essentials:purchase:view")
    @GetMapping()
    public String purchase()
    {
        return prefix + "/purchase";
    }

    /**
     * 跳转订货处理页面
     * @return
     */
    @RequiresPermissions("essentials:purchase:view")
    @GetMapping("/dispose")
    public String purchaseDetails(){
        return prefix + "/dispose";
    }

    /**
     * 查询进货订单列表
     */
    @RequiresPermissions("essentials:purchase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsPurchase wmsPurchase)
    {
        startPage();
        List<WmsPurchase> list = wmsPurchaseService.selectWmsPurchaseList(wmsPurchase);
        return getDataTable(list);
    }

    /**
     * 处理进货订单列表
     */
    @RequiresPermissions("essentials:purchase:list")
    @PostMapping("dispose/list")
    @ResponseBody
    public TableDataInfo disposeList(WmsPurchase wmsPurchase)
    {
        startPage();
        wmsPurchase.setPurStatus("未提交");
        List<WmsPurchase> list = wmsPurchaseService.selectWmsPurchaseList(wmsPurchase);
        return getDataTable(list);
    }

    /**
     * 导出进货订单列表
     */
    @RequiresPermissions("essentials:purchase:export")
    @Log(title = "进货订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsPurchase wmsPurchase)
    {
        List<WmsPurchase> list = wmsPurchaseService.selectWmsPurchaseList(wmsPurchase);
        ExcelUtil<WmsPurchase> util = new ExcelUtil<WmsPurchase>(WmsPurchase.class);
        return util.exportExcel(list, "purchase");
    }

    /**
     * 下单选择商品页面
     */
    @GetMapping("/addProducts")
    public String addProduct(){
        return prefix + "/addProducts";
    }

    /**
     * 新增商品到订货单
     */
    @ResponseBody
    @PostMapping("/addProduct")
    public AjaxResult addProduct(Integer pId, Integer purId, Integer detPurPCount){
        String mess=wmsPurchaseService.updatePurOrder(pId, purId, detPurPCount);
        if ("ok".equals(mess)){
            return success();
        }else{
            return error(mess);
        }
    }

    /**
     *跳转商品下单
     */
    @GetMapping("addProducts/{pId}")
    public String editProducts(@PathVariable("pId")Integer pId,ModelMap modelMap){
        WmsProduct wmsProduct =wmsProductService.selectWmsProductById(pId);
        List<WmsPurchase> wmsPurchases =wmsPurchaseService.selectWmsPurchaseList(new WmsPurchase());
        modelMap.put("wmsPurchases", wmsPurchases);
        modelMap.put("wmsProduct",wmsProduct);
        return prefix + "/orders";
    }

    /**
     * 新增保存进货订单
     */
    @RequiresPermissions("essentials:purchase:add")
    @Log(title = "进货订单", businessType = BusinessType.INSERT)
    @PostMapping("/addPurchase")
    @ResponseBody
    public Boolean addPurchase()
    {
        WmsPurchase wmsPurchase =new WmsPurchase();
        wmsPurchase.setPurStatus("未提交");
        wmsPurchase.setPurSubmittime(new Date());
        Integer row=wmsPurchaseService.insertWmsPurchase(wmsPurchase);
        return row>0;
    }

    /**
     * 修改进货订单
     */
    @GetMapping("/edit/{purId}")
    public String edit(@PathVariable("purId") Integer purId, ModelMap mmap)
    {
        WmsPurchase wmsPurchase = wmsPurchaseService.selectWmsPurchaseById(purId);
        mmap.put("wmsPurchase", wmsPurchase);
        return prefix + "/edit";
    }

    /**
     * 修改保存进货订单
     */
    @RequiresPermissions("essentials:purchase:edit")
    @Log(title = "进货订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsPurchase wmsPurchase)
    {
        return toAjax(wmsPurchaseService.updateWmsPurchase(wmsPurchase));
    }

    /**
     * 删除进货订单
     */
    @RequiresPermissions("essentials:purchase:remove")
    @Log(title = "进货订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsPurchaseService.deleteWmsPurchaseByIds(ids));
    }

    /**
     * 根据id完成提交
     */
    @ResponseBody
    @RequestMapping("/disposeSub")
    public Boolean disposeSub(Integer id){
        int i=wmsPurchaseService.updateDisposeSub(id);
        return i>0;
    }
}
